package com.tedu.day07_springmvc.controller;

import com.tedu.day07_springmvc.mapper.IUserMapper;
import com.tedu.day07_springmvc.pojo.dto.UserDTO;
import com.tedu.day07_springmvc.pojo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class UserController {
    @Autowired
    private IUserMapper iUserMapper;
/*    @RequestMapping("/regUser")
    @ResponseBody
    public String regUser(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        int age = Integer.parseInt(ageStr);
        System.out.println("开始处理用户注册");
        return "注册成功！";
    }*/

    /*@RequestMapping("/regUser")
    @ResponseBody
    public String regUser(String username,String password,String nickname,int age){
        System.out.println("注册信息："+username+","+password+","+nickname+","+age);
        return "注册成功";

    }*/

    @RequestMapping("/regUser")
    @ResponseBody
    public String regUser(UserDTO userDTO){

        System.out.println(userDTO);

        User user = new User();
        user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        user.setNickname(userDTO.getNickname());
        user.setAge(userDTO.getAge());
        user.setCreated(new Date());
        int rows = iUserMapper.insertUser(user);
        return rows>0?"注册成功":"注册失败";

    }
}
